/*
============================================================================
 Name        : TestDBAppUi.cpp
 Author      : 
 Copyright   : Your copyright notice
 Description : CTestDBAppUi implementation
============================================================================
*/

// INCLUDE FILES
#include <avkon.hrh>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <TestDB.rsg>
#include <f32file.h>
#include <s32file.h>

#include "mk4.h"
#include "mk4str.h"

#include "TestDB.pan"
#include "TestDBAppUi.h"
#include "TestDBAppView.h"
#include "TestDB.hrh"

_LIT( KFileName, "C:\\private\\E9A3ABFB\\TestDB.txt" );
_LIT( KText, "Hello World!");

// ============================ MEMBER FUNCTIONS ===============================
int FieldIntValue(c4_View* view, char* fieldname, int pos)
{
	c4_IntProp intfield(fieldname);
	int value = intfield((*view)[pos]);
	return value;
}

void SetRowRefIntValue(c4_RowRef rowref, char* fieldname, int value)
{
	c4_IntProp intfield(fieldname);
	intfield(rowref) = value;
}

TPtrC8 FieldUtf8Value(c4_View* view, char* fieldname, int pos)
{
	c4_BytesProp binfield(fieldname);
	c4_Bytes bin;
CEikonEnv::Static()->InfoWinL(_L("test"), _L("i"));		
	bin = binfield(((*view)[pos]));
CEikonEnv::Static()->InfoWinL(_L("test"), _L("2"));			

	const int len = bin.Size();
	if(len == 0) return _L8("");
	
	TPtrC8 ptr8((TUint8*)bin.Contents(), len);
	return ptr8;
}


// -----------------------------------------------------------------------------
// CTestDBAppUi::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//

void CTestDBAppUi::ConstructL()
    {
    // Initialise app UI with standard value.
    BaseConstructL();

	// Create view object
	iAppView = CTestDBAppView::NewL( ClientRect() );

	// Create a file to write the text to
	RFs fsSession;
	User::LeaveIfError(fsSession.Connect());
	CleanupClosePushL( fsSession );

	TInt err = fsSession.MkDirAll(KFileName);
	if ( (KErrNone != err) && (KErrAlreadyExists != err) )
		{
		CleanupStack::PopAndDestroy(1); //  fsSession
		return;
		}
	
	RFile file;
	err = file.Replace(fsSession, KFileName, EFileWrite );
	CleanupClosePushL( file );
	if ( KErrNone != err )
		{
		CleanupStack::PopAndDestroy(2); // file, fsSession
		return;
		}
	
	RFileWriteStream outputFileStream( file );
	CleanupClosePushL( outputFileStream );
	outputFileStream << KText;

	CleanupStack::PopAndDestroy(3); // outputFileStream, file, fsSession

	char infopath[] = "c:\\data\\test\\info.bdb";
	c4_Storage* iDBInfo = new c4_Storage(infopath, true);//?????	
	
	c4_View view = iDBInfo->View("PageLargeImages");
	
	c4_Row row;

	int size = view.GetSize();
	for(int i = 0; i < size; i++)
	{
		int nn = FieldIntValue(&view, "id", i);
	}

	TPtrC8 ptr = FieldUtf8Value(&view, "data", 0);
			
	TBuf<10> tt;
	int nn = FieldIntValue(&view, "id", 1);
	tt.Num(nn);	
	CEikonEnv::Static()->InfoWinL(_L("test"), tt);
	int lastid = (size == 0)?0:FieldIntValue(&view, "id", size-1);

	SetRowRefIntValue(row, "id", 100);
	view.Add(row);
	
	iDBInfo->Commit();
	delete iDBInfo;
	}
// -----------------------------------------------------------------------------
// CTestDBAppUi::CTestDBAppUi()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CTestDBAppUi::CTestDBAppUi()
    {
    // No implementation required
    }

// -----------------------------------------------------------------------------
// CTestDBAppUi::~CTestDBAppUi()
// Destructor.
// -----------------------------------------------------------------------------
//
CTestDBAppUi::~CTestDBAppUi()
    {
    if ( iAppView )
        {
        delete iAppView;
        iAppView = NULL;
        }

    }

// -----------------------------------------------------------------------------
// CTestDBAppUi::HandleCommandL()
// Takes care of command handling.
// -----------------------------------------------------------------------------
//
void CTestDBAppUi::HandleCommandL( TInt aCommand )
    {
    switch( aCommand )
        {
        case EEikCmdExit:
        case EAknSoftkeyExit:
            Exit();
            break;

        case ECommand1:
            {
            
            // Load a string from the resource file and display it
            HBufC* textResource = StringLoader::LoadLC( R_COMMAND1_TEXT );
            CAknInformationNote* informationNote;

            informationNote = new ( ELeave ) CAknInformationNote;

            // Show the information Note with
            // textResource loaded with StringLoader.
            informationNote->ExecuteLD( *textResource);

            // Pop HBuf from CleanUpStack and Destroy it.
            CleanupStack::PopAndDestroy( textResource );
            }
            break;
		case ECommand2:
			{
			
			RFs fsSession;
			RFile rFile;
			
			// Connects a client process to the fileserver
			User::LeaveIfError(fsSession.Connect());
			CleanupClosePushL(fsSession);
			
			//Open file where the stream text is
			User::LeaveIfError(rFile.Open(fsSession,KFileName, EFileStreamText));//EFileShareReadersOnly));// EFileStreamText));
			CleanupClosePushL(rFile);
			
			// copy stream from file to RFileStream object
			RFileReadStream inputFileStream(rFile);
    		CleanupClosePushL(inputFileStream);
    		
    		// HBufC descriptor is created from the RFileStream object.
    		HBufC* fileData = HBufC::NewLC(inputFileStream, 32);

            CAknInformationNote* informationNote;

            informationNote = new ( ELeave ) CAknInformationNote;
            // Show the information Note
            informationNote->ExecuteLD( *fileData);			
			
			// Pop loaded resources from the cleanup stack
			CleanupStack::PopAndDestroy(4); // filedata, inputFileStream, rFile, fsSession
			fsSession.Close();
			}
			break;
        default:
            Panic( ETestDBUi );
            break;
        }
    }
// -----------------------------------------------------------------------------
//  Called by the framework when the application status pane
//  size is changed.  Passes the new client rectangle to the
//  AppView
// -----------------------------------------------------------------------------
//
void CTestDBAppUi::HandleStatusPaneSizeChange()
{
	iAppView->SetRect( ClientRect() );
	
} 

// End of File
